package com.example.reggie.exception;


import com.example.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> sql(SQLIntegrityConstraintViolationException ex){
        log.info("异常信息为:{}",ex.getMessage());
        if (ex.getMessage().contains("Duplicate entry")){
            String[] arr=ex.getMessage().split(" ");
            return R.error(arr[2]+"已存在");
        }
        return R.error("未知错误");
    }

    //不为空异常
    @ExceptionHandler(CustomException.class)
    public R<String> custom(CustomException ex){
        return R.error(ex.getMessage());
    }



}
